﻿using System;
using System.Data;
using System.Collections.Generic;
using Chenxin.Lib.Hr.Entity;
using Chenxin.Lib.Utility;
namespace Chenxin.Lib.Hr.BLL
{
    /// <summary>
    /// 业务逻辑类Hr_AttendanceRecord 的摘要说明。
    /// </summary>
    public class Hr_AttendanceRecord
    {
        //Cache服务类
        private Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.HR);

        public Hr_AttendanceRecord()
        { }
        #region  成员方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Chenxin.Lib.Hr.Entity.Hr_AttendanceRecord model)
        {
            Chenxin.Lib.Hr.DAL.Hr_AttendanceRecord.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Chenxin.Lib.Hr.Entity.Hr_AttendanceRecord model)
        {
            Chenxin.Lib.Hr.DAL.Hr_AttendanceRecord.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(Guid RecordId)
        {

            Chenxin.Lib.Hr.DAL.Hr_AttendanceRecord.Delete(RecordId);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Chenxin.Lib.Hr.Entity.Hr_AttendanceRecord GetModel(Guid RecordId)
        {

            return Chenxin.Lib.Hr.DAL.Hr_AttendanceRecord.GetModel(RecordId);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetListByUserMonth(Guid UserId, string Month)
        {
            string strWhere;

            strWhere = " UserId = '" + UserId.ToString() + "'";
            strWhere = strWhere + " And Month = '" + Month + "'";
            return Chenxin.Lib.Hr.DAL.Hr_AttendanceRecord.GetList(strWhere);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DataSet GetListByToday(Guid UserId, DateTime Today)
        {
            System.Text.StringBuilder strWhere = new System.Text.StringBuilder();

            strWhere.AppendFormat(" UserId = '{0}'", UserId.ToString());
            strWhere.AppendFormat(" And DATEDIFF(DAY,DateTime,'{0}')=0", Today.ToShortDateString());

            return Chenxin.Lib.Hr.DAL.Hr_AttendanceRecord.GetList(strWhere.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return Chenxin.Lib.Hr.DAL.Hr_AttendanceRecord.GetList(strWhere);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.Hr.Entity.Hr_AttendanceRecord> GetModelList(string strWhere)
        {
            DataSet ds = Chenxin.Lib.Hr.DAL.Hr_AttendanceRecord.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.Hr.Entity.Hr_AttendanceRecord> DataTableToList(DataTable dt)
        {
            List<Chenxin.Lib.Hr.Entity.Hr_AttendanceRecord> modelList = new List<Chenxin.Lib.Hr.Entity.Hr_AttendanceRecord>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Chenxin.Lib.Hr.Entity.Hr_AttendanceRecord model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Chenxin.Lib.Hr.Entity.Hr_AttendanceRecord();
                    if (dt.Rows[n]["RecordId"].ToString() != "")
                    {
                        model.RecordId = new Guid(dt.Rows[n]["RecordId"].ToString());
                    }
                    if (dt.Rows[n]["UserId"].ToString() != "")
                    {
                        model.UserId = new Guid(dt.Rows[n]["UserId"].ToString());
                    }
                    model.UserCode = dt.Rows[n]["UserCode"].ToString();
                    if (dt.Rows[n]["DepartId"].ToString() != "")
                    {
                        model.DepartId = new Guid(dt.Rows[n]["DepartId"].ToString());
                    }
                    model.Type = dt.Rows[n]["Type"].ToString();
                    model.Time = dt.Rows[n]["Time"].ToString();
                    model.Year = dt.Rows[n]["Year"].ToString();
                    model.Month = dt.Rows[n]["Month"].ToString();
                    if (dt.Rows[n]["DateTime"].ToString() != "")
                    {
                        model.DateTime = DateTime.Parse(dt.Rows[n]["DateTime"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        //*******************************************
        //功能概要       ：根据参数列表获得用户考勤信息一览
        //参数説明       ：context 请求的上下文
        //返回值説明     ：无
        //功能詳細       ：根据参数列表获得用户考勤信息一览
        //作成者         ：he.jifeng 
        //作成日         ：2010-03-18
        //*******************************************
        public DataSet GetModelListByCoditions(System.Collections.Hashtable hsParams)
        {
            //where文
            System.Text.StringBuilder strWhere = new System.Text.StringBuilder();

            //年月
            string strYearMonth = "";
            //权限
            string strPermit;

            //结果集
            DataSet dsRet;

            strWhere.Append(" 1=1 ");

            //权限
            if (hsParams.ContainsKey("Permit"))
            {
                strPermit = hsParams["Permit"].ToString();
                if (strPermit == Constants.Permit_ViewAllAttendance)
                {
                    //全部
                }
                else if (strPermit == Constants.Permit_ViewCityAttendance)
                {
                    strWhere.Append(" And EXISTS(");
                    strWhere.AppendFormat("Select 1 from Hr_DepartmentInfo M WHERE Hr_AttendanceRecord.DepartId=M.DepartId And M.CityId= '{0}'", hsParams["CurCityId"]);
                    strWhere.Append(") ");
                }
                else if (strPermit == Constants.Permit_ViewCompanyAttendance)
                {
                    strWhere.Append(" And EXISTS(");
                    strWhere.AppendFormat("Select 1 from Hr_DepartmentInfo M WHERE Hr_AttendanceRecord.DepartId=M.DepartId And M.CompanyId= '{0}'", hsParams["CurCompanyId"]);
                    strWhere.Append(") ");
                }
                else if (strPermit == Constants.Permit_ViewDepartAttendance)
                {
                    strWhere.Append(" And EXISTS(");
                    strWhere.AppendFormat("Select 1 from Struct_UserDepartRelation M WHERE Hr_AttendanceRecord.DepartId=M.DepartId And M.Userid= '{0}' And M.ViewAttendance=1 ", hsParams["CurUserId"]);
                    strWhere.Append(") ");
                }
            }
            //权限为当前用户
            else if (hsParams.ContainsKey("CurUserId"))
            {
                strWhere.Append(" And UserId = '" + hsParams["CurUserId"] + "'");
            }
            
            //用户名
            if (hsParams.ContainsKey("UserId"))
            {
                strWhere.Append(" And UserId = '" + hsParams["UserId"] + "'");
            }
            //部门
            else if (hsParams.ContainsKey("DepartId"))
            {
                strWhere.Append(" And DepartId = '" + hsParams["DepartId"] + "'");
            }
            //商圈
            else if (hsParams.ContainsKey("AreaId"))
            {
                Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo entArea = null;
                string strCachkey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_DEPARTMENT, hsParams["AreaId"]);
                cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);
                
                entArea = cs.GetObject<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo>(strCachkey);
                if (!cs.Hit)
                {
                    entArea = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetModel(new Guid(hsParams["AreaId"].ToString()));
                    if (entArea != null) cs.Set(strCachkey, entArea, DateTime.Now.AddDays(1));
                }

                if (entArea != null)
                {
                    strWhere.AppendFormat(" AND (DepartId in (select departId from Hr_DepartmentInfo where DepartSrc like '{0}%'))", entArea.DepartSrc);
                }
            }
            //公司
            else if (hsParams.ContainsKey("CompanyId"))
            {
                strWhere.AppendFormat(" AND (DepartId in (Select DepartId from Hr_DepartmentInfo where CompanyId = '{0}'))", hsParams["CompanyId"]);
            }
            //城市
            else if (hsParams.ContainsKey("CityId"))
            {
                strWhere.AppendFormat(" AND (DepartId in (Select DepartId from Hr_DepartmentInfo where CityId = '{0}'))", hsParams["CityId"]);
            }

            //年份
            if (hsParams.ContainsKey("Year"))
            {
                strYearMonth = hsParams["Year"].ToString();
                strWhere.Append(" And Year = '" + strYearMonth + "'");
            }
            //月份
            if (hsParams.ContainsKey("Month"))
            {
                strYearMonth = strYearMonth + hsParams["Month"].ToString();
                strWhere.Append(" And Month = '" + hsParams["Month"] + "'");
            }

            dsRet = Chenxin.Lib.Hr.DAL.Hr_AttendanceRecord.GetList(strWhere.ToString());

            return dsRet;
        }

        #endregion  成员方法
    }
}

